var table;
layui.use('table', function(){
    table = layui.table
        ,form = layui.form;

    table.render({
        elem: '#test'
        ,url:'/user/JsonUserList'
        ,cellMinWidth: 80
        ,cols: [[
            {type:'numbers'}
            ,{type: 'checkbox'}
            ,{field:'userId', title:'ID'}
            ,{field:'userName', title:'会员姓名'}
            ,{field:'phone', title:'联系电话'}
            ,{field:'cardId', title: '身份证'}
            ,{field:'recommend', title: '推荐人'}
            ,{field:'registerTime',width:160 , title: '注册时间',templet :function (row){return createTime(row.registerTime);}}
            ,{field:'sort', title: '排序'}
            ,{field:'id', title:'编辑', templet: '#barDemo', unresize: true}
        ]]
        ,page: true
        ,limit:10
        ,id: 'testReload'
    });

    //监听表格复选框选择
    table.on('checkbox(demo)', function(obj){
        console.log(obj)
    });
});

function createTime(v){
    return new Date(parseInt(v)).toLocaleString()
}
/**重写toLocaleString方法*/
Date.prototype.toLocaleString = function() {
    var y = this.getFullYear();
    var m = this.getMonth()+1;
    m = m<10?'0'+m:m;
    var d = this.getDate();
    d = d<10?("0"+d):d;
    var h = this.getHours();
    h = h<10?("0"+h):h;
    var M = this.getMinutes();
    M = M<10?("0"+M):M;
    var S=this.getSeconds();
    S=S<10?("0"+S):S;
    return y+"-"+m+"-"+d+" "+h+":"+M+":"+S;}


function WinOpenAdd() {
    layer.open({
        type: 2
        , title: '添加会员编号'
        , content: '/user/getUserAdd'
        , area: ['420px', '420px']
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
            debugger;
            var body = layer.getChildFrame('body', index); //得到iframe页的body内容
            var userName = body.find("#userName").val();
            var phone = body.find("#phone").val();
            var cardId = body.find("#cardId").val();
            var recommend = body.find("#recommend").val();
            var sort = body.find("#sort").val();

            var addUrl="/user/addUser?userName="+userName+"&phone="+phone+"&cardId="+cardId+"&recommend="+recommend+"&sort="+sort;
            alert(addUrl);
            var htmlobj=$.ajax({url:addUrl,async:false});
            alert(htmlobj);
            //数据刷新
            var demoReload = $('#demoReload');
            //执行重载
            table.reload('testReload', {
            });

            layer.close(index); //关闭弹层
        }
    });
}

function WinOpenEdit(id) {
    layer.open({
        type: 2
        , title: '编辑会员资料'
        , content: '/user/getUserEdit?userId='+id
        , area: ['420px', '420px']
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
            debugger;
            var body = layer.getChildFrame('body', index); //得到iframe页的body内容
            var userName = body.find("#userName").val();
            var phone = body.find("#phone").val();
            var cardId = body.find("#cardId").val();
            var recommend = body.find("#recommend").val();
            var sort = body.find("#sort").val();
            var addUrl="/user/editUser?userId="+id+"&userName="+userName+"&phone="+phone+"&cardId="+cardId+"&recommend="+recommend+"&sort="+sort;
            var htmlobj=$.ajax({url:addUrl,async:false});
            //执行重载
            table.reload('testReload', {

            });

            layer.close(index); //关闭弹层
        }
    });
}

function WinOpenShow(id) {
    layer.open({
        type: 2
        , title: '查看会员编号'
        , content: '/user/getUserShow?userId='+id
        , area: ['420px', '420px']
    });
}

function DelData(id) {
    layer.open({
        title: '消息'
        , content: '是否确定删除编号为：'+id+' 的数据!!'
        , btn: '确定删除'
        , yes: function () {
            var delUrl = "/user/delUser?userId=" + id;
            //alert(delUrl);
            var htmlobj = $.ajax({url: delUrl, async: false});
            //alert(htmlobj.responseText);
            //数据刷新
            var demoReload = $('#demoReload');
            //执行重载
            table.reload('testReload', {
            });
            layer.closeAll();
        }
    });
}


function SelectData() {
    //数据刷新
    var userNo = $('#userNo').val();
    var userName = $('#userName').val();
    //执行重载
    table.reload('testReload', {
        url:'/user/JsonUserList?userNo='+userNo+'&name='+userName,
        page: {
            curr: 1 //重新从第 1 页开始
        }
    });
}